package com.hjm.app.fragment.store;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hjm.app.R;

public class FragmentStoreDetail extends Fragment {
    private static final String TAG = "FragmentStoreHome";
    private WebView webViewStoreHome;
    private boolean isLoaded = false;
    private String detailHtmlContent = "<h1>店铺信息</h1>";

    public FragmentStoreDetail() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store_detail, container, false);
        webViewStoreHome = view.findViewById(R.id.web_view_store_detail);

        WebSettings settings = webViewStoreHome.getSettings();
        settings.setBuiltInZoomControls(false);//设定是否显示缩放按钮
        settings.setSupportZoom(false);//设定是否支持缩放
        settings.setJavaScriptEnabled(true);// 启用JavaScript支持
        settings.setDomStorageEnabled(true);

        /**
         * WebChromeClient类用于辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等。
         * 打印webview日志
         */
        webViewStoreHome.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                Log.d("WebView", "控制台信息: " + consoleMessage.message() + " -- From line "+ consoleMessage.lineNumber());
                return true; // 表示您已经处理了这条消息
            }
        });

        /**
         * WebViewClient类用于辅助WebView处理各种通知、请求事件。
         * 页面渲染完成时执行
         */
        webViewStoreHome.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        webViewStoreHome.loadDataWithBaseURL(null, this.detailHtmlContent, "text/html", "UTF-8", null);

        Log.d(TAG, "onCreateView: store_home.html");
        return view;
    }
    public void setDetail(String detailHtmlContent){
        this.detailHtmlContent = detailHtmlContent;
    }
}